package com.leanyuan.Infor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.Infor.api.Result;
import com.leanyuan.publi.domain.*;
import com.leanyuan.publi.dto.Order2Dto;
import com.leanyuan.publi.dto.PayitemsDto;
import com.leanyuan.publi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoufei")
public class ShoufeiController {
    @Autowired
    PayitemsService payitemsService;

    @Autowired
    BuildingService buildingService;

    @Autowired
    HouseService houseService;

    @Autowired
    OrdersService ordersService;

    @Autowired
    Order2Service order2Service;

    @RequestMapping("selectByOrderid")
    public Result<Object> selectByOrderid(@RequestBody Map<String,Integer> param){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("Order_id",param.get("id"));
        Page page=new Page(param.get("page"),param.get("pageSize"));
        IPage page1=order2Service.page(page,wrapper);
        return Result.ok(page1);
    }
    @RequestMapping("jiaofei2")
    public Result<Object> jiaofei2(@RequestBody Order2Dto order2Dto){
        List<Integer> list=order2Dto.getId();
        QueryWrapper wrapper=new QueryWrapper();
        Order2 order2=new Order2();
        order2.setStatus("已缴费");
        order2.setIsInvoice(order2Dto.getIs_invoice());
        order2.setCost(new Date());
        for (Integer i:list) {
            wrapper.eq("id",i);
            order2Service.update(order2,wrapper);
        }
        return Result.ok();
    }
    @RequestMapping("jiaofei")
    public Result<Object> jiaofei(@RequestBody PayitemsDto payitemsDto){
        List<Order2> list=order2Service.findByOrderid(payitemsDto.getHouseId());
        return Result.ok(list);
    }
    @RequestMapping("add3")
    public Result<Object> add3(@RequestBody PayitemsDto payitemsDto){
        QueryWrapper pwrapper=new QueryWrapper();
        pwrapper.eq("Pname",payitemsDto.getPname());
        Payitems payitems=payitemsService.getOne(pwrapper);
        Orders orders=new Orders();
        orders.setHouseId(payitemsDto.getHouseId());
        orders.setStatus("收费中状态");
        orders.setPId(payitems.getPId());
        orders.setStart(payitemsDto.getDate());
        Boolean flag=ordersService.save(orders);
        if(flag){
            return Result.ok();
        }
        return null;
    }
    @RequestMapping("upd2")
    public Result<Object> upd2(@RequestBody Map<String,Integer> param){
        Orders orders=new Orders();
        orders.setStatus("收费结束");
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("Order_id",param.get("id"));
        boolean flag=ordersService.update(orders,wrapper);
        if(flag){
            return Result.ok();
        }
        return null;
    }
    @RequestMapping("findOrder")
    public Result<Object> findOrder(@RequestBody Map<String,Integer> param){
        List<Orders> list=ordersService.findByid(param.get("id"));
        return Result.ok(list);
    }
    @RequestMapping("add2")
    public Result<Object> add2(@RequestBody PayitemsDto payitemsDto){
        //根据name得到id
        QueryWrapper bwrapper=new QueryWrapper();
        bwrapper.eq("name",payitemsDto.getLname());
        Building building=buildingService.getOne(bwrapper);

        QueryWrapper hwrapper=new QueryWrapper();
        if(payitemsDto.getLname()!="全部") {
            hwrapper.eq("building_id", building.getBuildingId());
        }
        hwrapper.eq("h_condition",payitemsDto.getRadio());
        List<House> list=houseService.list(hwrapper);

        QueryWrapper pwrapper=new QueryWrapper();
        pwrapper.eq("Pname",payitemsDto.getPname());
        Payitems payitems=payitemsService.getOne(pwrapper);

        Orders orders=new Orders();
        orders.setPId(payitems.getPId());
        orders.setStart(payitemsDto.getDate());
        orders.setStatus("0");
        orders.setStatus("收费中状态");
        for (House h:list) {
            orders.setHouseId(h.getHouseId());
            ordersService.save(orders);
        }
        return Result.ok();
    }
    @RequestMapping("select")
    public Result<Object> select(){
        List list=payitemsService.list(null);
        return Result.ok(list);
    }
    @RequestMapping("del")
    public Result<Object> del(@RequestBody Map<String,Integer> param){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("P_id",param.get("id"));
        boolean flag=payitemsService.remove(wrapper);
        if(flag){
            return Result.ok();
        }
        return null;
    }
    @RequestMapping("upd")
    public Result<Object> upd(@RequestBody Payitems payitems){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("P_id",payitems.getPId());
        boolean flag=payitemsService.update(payitems,wrapper);
        if(flag){
            return Result.ok();
        }
        return null;
    }
    @RequestMapping("/add")
    public Result<Object> add(@RequestBody Payitems payitems){
        payitems.setDelete1(0);
        boolean flag=payitemsService.save(payitems);
        if(flag){
            return Result.ok();
        }
        return null;
    }
    @RequestMapping("/findAll")
    public Result<Object> findAll(@RequestBody Map<String,Integer> param){
        Page<Payitems> page=new Page<Payitems>(param.get("page"),param.get("pagesize"));
        IPage page1=payitemsService.page(page,null);
        return Result.ok(page1);
    }
}
